/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.activity.DetailGoodsActivity;
import com.epfresh.activity.HomeSecondAreaActivity;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.bean.HomeTop;

import java.util.Collections;
import java.util.List;

/**
 * @author drakeet
 */
public class ViewBinderHomeSecondItemAdapter extends RecyclerView.Adapter<ViewBinderHomeSecondItemAdapter.ViewHolder>  implements View.OnClickListener{

    private List<HomeTop.SeckillBeanX.SeckillBean.SeckillListBean> posts = Collections.emptyList();


    public void setPosts(@NonNull List<HomeTop.SeckillBeanX.SeckillBean.SeckillListBean> posts) {
        this.posts = posts;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.view_home_second_item, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onClick(View v) {
        Object position = v.getTag(R.id.item_key_position);
        if (position!=null&&position instanceof Integer&&(Integer)position == posts.size()-1)
        {
            Intent more_activity = new Intent(v.getContext(),
                    HomeSecondAreaActivity.class);
            v.getContext().startActivity(more_activity);
            return;
        }

        Object tag = v.getTag(R.id.item_key_status);
        if (tag != null && tag instanceof HomeTop.SeckillBeanX.SeckillBean.SeckillListBean) {
            HomeTop.SeckillBeanX.SeckillBean.SeckillListBean bean = (HomeTop.SeckillBeanX.SeckillBean.SeckillListBean) tag;
            Intent commIntent = new Intent(v.getContext(), DetailGoodsActivity.class);
            commIntent.putExtra("id", bean.getProduct().getId());
            commIntent.putExtra("image_url", bean.getProduct().getThumbnail());
            commIntent.putExtra("promotionItemId", bean.getPromotionItemId());
            commIntent.putExtra("title", bean.getProduct().getTitle());
            commIntent.putExtra("storeId", bean.getProduct().getStoreId() + "");
            v.getContext().startActivity(commIntent);
        }
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        HomeTop.SeckillBeanX.SeckillBean.SeckillListBean bean = posts.get(position);
        if(position == posts.size()-1){
            holder.iv_product.setVisibility(View.VISIBLE);
            holder.tv_old_price.setVisibility(View.VISIBLE);
            holder.tv_center_more.setVisibility(View.VISIBLE);
            holder.rl_center_more.setTag(R.id.item_key_position,position);
            holder.rl_center_more.setVisibility(View.VISIBLE);
            holder.tv_center_number.setText("共"+(posts.get(position).getPromotionId())+"件");
            holder.rl_center_more.setOnClickListener(this);
            return;
        }else {
            holder.rl_center_more.setVisibility(View.GONE);
        }

        if(bean==null||bean.getProduct()==null) {
            return;
        }
        holder.tv_price.setText(""+bean.getPrice()+"元/"+bean.getProduct().getChargeUnit());
        holder.tv_old_price.setText(bean.getProduct().getProductSpecPrice().getPrice() + "元/" + bean.getProduct().getChargeUnit());
        holder.tv_old_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

        ImageLoader.getInstance().displayImage(bean.getProduct().getThumbnail(), holder.iv_product);
        if (bean.getPromotionMarkUrl() != null && !bean.getPromotionMarkUrl().equals("") && !bean.getPromotionMarkUrl().equals("null"))
            ImageLoader.getInstance().displayImage(bean.getPromotionMarkUrl(), holder.iv_tag);
        else
            holder.iv_tag.setVisibility(View.INVISIBLE);

        holder.itemView.setTag(R.id.item_key_status,bean);


        holder.itemView.setOnClickListener(this);


        if (bean.getStatus() != null && "2".equals(bean.getStatus()))
            holder.tv_finish_flag.setVisibility(View.GONE);
        else if ("1".equals(bean.getStatus())) {
            holder.tv_finish_flag.setVisibility(View.VISIBLE);
            holder.tv_finish_flag.setText("即将开始");
        } else if ("3".equals(bean.getStatus())) {
            holder.tv_finish_flag.setVisibility(View.VISIBLE);
            holder.tv_finish_flag.setText("结束了");
        } else {
            holder.tv_finish_flag.setVisibility(View.VISIBLE);
            holder.tv_finish_flag.setText("抢光了");
        }
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView iv_product,iv_tag;
        TextView tv_price,tv_old_price,tv_finish_flag;


        private TextView tv_center_number;
        private View rl_center_more,tv_center_more;

        ViewHolder(View itemView) {
            super(itemView);
            iv_product = (ImageView) itemView.findViewById(R.id.iv_product);
            iv_tag = (ImageView) itemView.findViewById(R.id.iv_tag);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
            tv_old_price = (TextView) itemView.findViewById(R.id.tv_old_price);
            tv_finish_flag = (TextView) itemView.findViewById(R.id.tv_finish_flag);
            tv_center_number = (TextView) itemView.findViewById(R.id.tv_center_number);
            rl_center_more =  itemView.findViewById(R.id.rl_center_more);
            tv_center_more =  itemView.findViewById(R.id.tv_center_more);
        }
    }
}
